package com.neuedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.neuedu.pojo.Permission;
import com.neuedu.pojo.RolePermission;
import com.neuedu.mapper.RolePermissionMapper;
import com.neuedu.service.IPermissionService;
import com.neuedu.service.IRolePermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 角色权限 服务实现类
 * </p>
 *
 * @author wgq
 * @since 2020-09-18
 */
@Service
public class RolePermissionServiceImpl extends ServiceImpl<RolePermissionMapper, RolePermission> implements IRolePermissionService {
    @Resource
    IPermissionService permissionService;
    @Override
    public List<Permission> getPermission() {
        QueryWrapper<Permission> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",0);
        wrapper.eq("active",1);
        List<Permission> list = permissionService.list(wrapper);
        for(Permission permission : list) {
            getChildren(permission);
        }
        return list;
    }

    @Override
    public RolePermission getByRoleId(Integer roleId) {
        QueryWrapper<RolePermission> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id",roleId);
        return this.getOne(wrapper);
    }

    /**
     * 参数为父权限对象
     * 通过父权限获取子权限
     *
     * */
    private void getChildren(Permission Permission) {
        QueryWrapper<Permission> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",Permission.getId());
        wrapper.eq("active",1);
        List<Permission> list = permissionService.list(wrapper);
        for(Permission permission : list) {
//            if(permission.getType() != 0) {
            getChildren(permission);
//            }
        }
        Permission.setChildren(list);
    }
}
